package com.n9.product.dao;

import com.n9.base.dao.BaseRepository;
import com.n9.product.entity.Patent;
import com.n9.product.entity.SaleStatus;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

/**
 * 专利产品DAO接口
 *
 * @author xqlu
 * @version 2019-01-23
 */
@Repository
public interface PatentDao extends BaseRepository<Patent, String> {
    /**
     * 判断专利号是否已经存在
     *
     * @param no
     * @return
     */
    boolean existsByNo(String no);

    /**
     * 根据专利号查询
     * @param no
     * @return
     */
    Patent findOneByNo(String no);

    /**
     * 根据供应商修改状态
     * @param providerId
     * @param saleStatus
     * @param newStatus
     */
    @Modifying
    @Query(value = "UPDATE n9_product SET sale_status = :newStatus WHERE id IN (SELECT id FROM n9_product_patent WHERE provider_id = :providerId ) AND sale_status = :saleStatus", nativeQuery = true)
    void updateSalestatusByProviderId(@Param("providerId") String providerId, @Param("saleStatus")String saleStatus, @Param("newStatus")String newStatus);

    /**
     * 根据销售状态查询
     * @param saleStatus
     * @return
     */
    Iterable<Patent> findAllBySaleStatus(SaleStatus saleStatus);

    /**
     * 查询没名称或者状态的专利
     * @return
     */
    @Query("FROM Patent WHERE name IS NULL OR status IS NULL")
    Iterable<Patent> findAllNoInfoNos();
}
